// pages/news_content/news_content.js

import config from './../../config';
import {
    empty,
    message,
    session
} from './../../utils/helper';
import {
    getArticleInfo,
    setArticleLike
} from './../../services/news_content';
import qs from 'qs';

//获取应用实例
const app = getApp();
//实例化本地缓存
const cookie = new session();

Page({

    //页面的初始数据
    data: {
        host: config.service.host,
        article: {},
    },

    //父级页面传递过来的表单参数
    params: {},

    //事件处理函数
    onLoad: function(options) {
        if (empty(options)) {
            console.warn('同学：你这个样子直接打开，是不得行的！');
        } else {
            this.params = options;
        }
        //显示当前页面的转发按钮
        wx.showShareMenu({
            withShareTicket: true
        });
        let that = this;
        //获取文章列表
        getArticleInfo(this.params, {
            success: res => {
                wx.setNavigationBarTitle({
                    title: res.data.info.meta_title //页面标题为路由参数
                })
                that.setData({
                    article: res.data.info
                });
            }
        });
    },

    //点赞
    like(e) {
        let that = this,
            params = e.currentTarget.dataset;
        setArticleLike(params, {
            success: res => {
                wx.showToast({
                    title: '点赞成功',
                    icon: 'none',
                    duration: 2000
                })
                that.setData({
                    article: Object.assign(that.data.article, {
                        like: that.data.article.like + 1
                    })
                });
            }
        });
    },

    //下拉刷新事件
    onPullDownRefresh() {
        this.onLoad();
        wx.stopPullDownRefresh();
    }
})